package com.itheima.info.manager.system.dao;

import com.itheima.info.manager.system.domain.Student;

import java.util.ArrayList;

public class StudentDaoImpl implements IStudentDao {

    private static ArrayList<Student>  list= new ArrayList<>();

    static{
        Student stu1 = new Student("0001", "张三1", 20, "23");
        Student stu2 = new Student("0002", "张三2", 22, "23");
        Student stu3 = new Student("0003", "张三3", 25, "23");
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);
    }
    @Override
    public void addStudent(Student student) {
        list.add(student);
    }

    @Override
    public void deleteStudent(String id) {
        int index = getIndex(id);
        list.remove(index);
    }



    @Override
    public void updateStudent(String id, Student newStu) {
        int index = getIndex(id);
        list.set(index,newStu);


    }

    @Override
    public ArrayList<Student> findAllStudent() {
        return list;
    }

    private int getIndex(String id) {
        int index=-1;
        for (int i = 0; i < list.size(); i++) {
            //list.get(i)
            Student student = list.get(i);
            if(student!=null&& student.getId().equals(id)){
                index=i;
                break;
            }
        }
        return index;
    }
}
